package com.epfresh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.bean.Address;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class AddressAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;


    private List<Address> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    private int currentIndex = -1;

    private String addressId;

    public AddressAdapter(Context context, List<Address> list) {
        this.context = context;
        this.list = list;
    }


    public String getAddressId() {
        return addressId;
    }

    public void setAddressId(String addressId) {
        this.addressId = addressId;
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_adress, null, false);
            vh.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            vh.tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
            vh.tvAddress = (TextView) convertView.findViewById(R.id.tv_address);
//            vh.tvNickname = (TextView) convertView.findViewById(R.id.tv_nickname);
//            vh.vDelete = convertView.findViewById(R.id.tv_delete);
//            vh.vEdit = convertView.findViewById(R.id.tv_edit);
            vh.img_address_edit = convertView.findViewById(R.id.img_address_edit);
//            vh.iv = (ImageView) convertView.findViewById(R.id.iv_select);
            vh.img_address_select = convertView.findViewById(R.id.img_address_select);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        Address address = list.get(position);
        if (!address.isHaveSelect()){
            vh.img_address_select.setVisibility(View.GONE);
        }else {
            vh.img_address_select.setVisibility(View.VISIBLE);
            if (currentIndex == position) {
                vh.img_address_select.setImageResource(R.mipmap.icon_address_selected);
//                vh.iv.setVisibility(View.VISIBLE);
            } else {
                vh.img_address_select.setImageResource(R.mipmap.icon_address_unselected);
//                vh.iv.setVisibility(View.INVISIBLE);
            }
        }


        vh.tvName.setText(address.getName());

        vh.tvPhone.setText(address.getPhone());
        vh.tvAddress.setText(address.getAddressStr());
//        vh.tvNickname.setText(ApplicationHelper.getInstance().getUser().getNickname());

//        vh.vEdit.setTag(R.id.item_key_position, position);
        vh.img_address_edit.setTag(R.id.item_key_position, position);
//        vh.vDelete.setTag(R.id.item_key_position, position);

//        vh.vEdit.setOnClickListener(this);
        vh.img_address_edit.setOnClickListener(this);
//        vh.vDelete.setOnClickListener(this);

        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        calCurrentIndex();
        super.notifyDataSetChanged();
    }

    public int calCurrentIndex() {
        currentIndex = -1;
        for (int i = 0; i < list.size(); i++) {
            Address address = list.get(i);

            String id = address.getId();

            if (id != null && !"".equals(id) && id.equals(addressId)) {
                currentIndex = i;
                break;
            }


        }
        return currentIndex;
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
            if (onItemClickListener != null) {
                int position = (int) o;
                if (v.getId() == R.id.tv_delete) {
                    onItemClickListener.onItemClick(null, v, position, 1);
                } else if (v.getId() == R.id.img_address_edit) {
                    onItemClickListener.onItemClick(null, v, position, 2);
                }
            }
        }
    }

    public class ViewHolder {
        TextView tvName;
        TextView tvPhone;
        TextView tvAddress;
        TextView tvNickname;
        View vEdit;
        View vDelete;
        ImageView iv,img_address_select,img_address_edit;
    }
}
